<?php
class SourceModel
{
    public function __construct()
    {
        $this->db = Yaf_Registry::get('db');
        $this->time = date("Y-m-d H:i:s",time());
    }

    public function selectSource($start,$everyPage)
    {
        $sql = "select * from `collection_source` order by source_id desc  limit {$start},{$everyPage}";
        return $this->db->query($sql);
    }

    public function getCollWebsiteinfo()
    {
        $sql = "select * from `collection_sites` ";
        return $this->db->query($sql);
    }

    public function getCollWebsiteByRobot($id)
    {
        $sql = "select DISTINCT website_id from collection_robot where article_type_id={$id} ";
        return $this->db->query($sql);
    }

    public function getCollRobotByWebsite($info)
    {
        $sql = "select * from collection_robot where website_id={$info['website_id']} and article_type_id={$info['article_type_id']} ";
        return $this->db->query($sql);
    }

    public function getTypicalNameByRobot($info)
    {
        $sql = "select * from article_type,collection_robot where article_type.`name`=collection_robot.robot and article_type.`name`='{$info[0]['robot']}' and collection_robot.website_id={$info[0]['website_id']}";
        return $this->db->query($sql);
    }

    public function getCollTypicalByRobot($id)
    {
        $sql = "select * from collection_robot where robot_id={$id} ";
        return $this->db->query($sql);
    } 

    public function getSourceInfo()
    {
        $sql = "select * from `collection_source` ";
        return $this->db->query($sql);
    }

    public function getAllCount()
    {
        $sql="select count(*) as `rows` from `collection_source` ";
        return $this->db->query($sql);
    }

    public function getMaxId()
    {
        $sql="select MAX(id) as id from collection_source ";
        return $this->db->query($sql);
    }

    public function del($id)
    {
        $sql = "delete from collection_source where source_id='{$id}' ";
        return $this->db->query($sql);
    }

    public function add($info)
    {
        $sql="insert into collection_source(`name`,`article_type_id`,`site_id`,`robot_id`,`url`,`remark`,`create_time`) values('{$info['name']}','{$info["article_type_id"]}','{$info["website_id"]}','{$info["robot"]}','{$info["url"]}','{$info["remark"]}',UNIX_TIMESTAMP('{$this->time}'))";
        return $this->db->query($sql);
    }

    public function edit($info, $id)
    {
        $sql = "update `collection_source` set `name`='{$info['name']}',`article_type_id`='{$info['article_type_id']}', `site_id`='{$info['source']}',`robot_id`='{$info['robot_id']}',`url`='{$info['url']}',`remark`='{$info['remark']}',`create_time`=UNIX_TIMESTAMP('{$this->time}') WHERE `source_id`='{$id}'";
        return $this->db->query($sql);
    }
    

}